package com.sugree.thairath.breakingnews;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.Spanned;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.util.Log;

import com.sugree.util.entity.Message;
import com.sugree.util.DownloadTasks;

public class BreakingNewsItem extends Activity {
	protected static final String TAG = "BreakingNewsItem";

	protected TextView mTitle;
	protected TextView mAuthor;
	protected TextView mPubDate;
	protected TextView mContent;
	protected ProgressBar mProgressBar;
	protected Button mBrowse;
	protected Button mShare;

	protected Message mMessage;
	protected Spanned mSpanned;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item);

		mTitle = (TextView)findViewById(R.id.title);
		mAuthor = (TextView)findViewById(R.id.author);
		mPubDate = (TextView)findViewById(R.id.pubdate);
		mContent = (TextView)findViewById(R.id.body);
		mProgressBar = (ProgressBar)findViewById(R.id.progressbar);

		mBrowse = (Button)findViewById(R.id.button_browse);
		mShare = (Button)findViewById(R.id.button_share);

		mBrowse.setOnClickListener(mButtonListener);
		mShare.setOnClickListener(mButtonListener);

		mMessage = (Message)getIntent().getParcelableExtra("item");
		if (savedInstanceState != null) {
			mSpanned = (Spanned)savedInstanceState.getCharSequence("spanned");
		}

		mTitle.setText(mMessage.getTitle());
		mAuthor.setText(mMessage.getAuthor());
		mPubDate.setText(mMessage.getPubDate().toString());
		mContent.setMovementMethod(LinkMovementMethod.getInstance());

		Log.d(TAG, "create");
		if (mSpanned != null) {
			mContent.setText(mSpanned);
		} else {
			new ContentParserTask().execute(this);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		nm.cancelAll();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putCharSequence("spanned", mSpanned);
	}

	protected View.OnClickListener mButtonListener = new View.OnClickListener() {
		public void onClick(View v) {
			Intent intent;
			if (v == mBrowse) {
				intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mMessage.getLink().toString()));
				startActivity(intent);
			} else if (v == mShare) {
				String text = mMessage.getTitle()+" "+mMessage.getLink();
				intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/plain");
				intent.putExtra(Intent.EXTRA_TEXT, text);
				startActivity(Intent.createChooser(intent, getText(R.string.get_share)));
			}
		}
	};

	protected class ContentParserTask extends AsyncTask<Object, Integer, Spanned> {
		protected void onPreExecute() {
			mProgressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progress_large));
			mProgressBar.setVisibility(View.VISIBLE);
		}

		protected Spanned doInBackground(Object... args) {
			String text = mMessage.getDescription();
			if (C.config.show_enclosure) {
				for(String enc : mMessage.getEnclosures()) {
					text += "<p><img src=\""+enc+"\"></p>";
				}
			}
			return Html.fromHtml(text, new Html.ImageGetter() {
				public Drawable getDrawable(String source) {
					return DownloadTasks.getDrawable(BreakingNewsItem.this.getResources(), source);
				}
			}, null);
		}

		protected void onProgressUpdate(Integer... progress) {
		}

		protected void onPostExecute(Spanned result) {
			mProgressBar.setVisibility(View.GONE);
			mSpanned = result;
			mContent.setText(result);
		}
	}
}
